<template>
  <div>
    <MyTable :list="list">

      <template #toubu>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>

      <template #weibu="{row}">
        <td>{{row.id}}</td>
        <td>{{row.goods_name}}</td>
        <td>{{row.goods_price}}</td>

        <td>
          <input @keydown.enter="enterFn(row)" @keydown.esc="row.inputValue=''" v-model="row.inputValue"
            @blur="row.inputVisible=false" v-fff v-if="row.inputVisible" type="text" class="tag-input form-control"
            style="width: 100px;">

          <button @click="row.inputVisible=true" v-else style="display:block;"
            class="btn btn-primary btn-sm add-tag">123</button>
          <span v-for="(item,index) in row.tags " :key="index">{{item}}</span>
        </td>
        <td><button class="btn btn-danger btn-sm" @click="subFn(row.id)">删除</button></td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../components/MyTable";

export default {
  components: {
    MyTable,
  },
  data() {
    return {
      list: [] // 所有数据
    };
  },
  created() {
    this.$axios({
      url: '/api/goods',
    }).then(ser => {
      this.list = ser.data.data
    }).catch(err => {
      console.log(err);
    })
  },
  methods: {
    //删除
    subFn(id) {
      this.list = this.list.filter(item => item.id !== id)
    },
    enterFn(row){
      if(row.inputValue.trim()==='') return alert('输入格式有误')
      row.tags.push(row.inputValue)
    }
  },
  directives: {
    fff: {
      inserted(el) {
        el.focus()
      }
    }
  }
};
</script>